package org.example.service.impl;


import lombok.RequiredArgsConstructor;
import org.example.domain.entity.LoginUser;
import org.example.domain.entity.User;
import org.example.mapper.MenuMapper;
import org.example.mapper.UserMapper;
import org.example.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final UserMapper userMapper;

    private final MenuService menuService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userMapper.SelectByUserName(username);

        if(Objects.isNull(user)){
            throw new RuntimeException("用户不存在");
        }

        if(user.getType() == 1){
            List<String> permsList = menuService.selectPermsByUserId(user.getId());
            return  new LoginUser(user,permsList);
        }
        //TODO 查询对应权限信息

        return  new LoginUser(user,null);
    }
}
